//
// Copyright 2021 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
@use 'sass:list';
@use 'sass:math';
@use 'sass:meta';
@use '@material/feature-targeting/feature-targeting';
@use '@material/rtl/rtl';
@use '@material/dom/dom';
@use '@material/shape/shape';
@use '@material/theme/gss';

$ring-radius-default: 8px !default;
$inner-ring-width-default: 2px !default;
$inner-ring-color-default: transparent !default;
$outer-ring-width-default: 2px !default;
$outer-ring-color-default: transparent !default;
$container-outer-padding-default: 2px !default;

/// Styles applied to the component's inner focus ring element.
///
/// @param $ring-radius [$ring-radius-default] - Focus ring radius, either a
////     single value or a list of radius values to apply.
/// @param $inner-ring-width [$inner-ring-width-default] - Inner focus ring width.
/// @param $inner-ring-color [$inner-ring-color-default] - Inner focus ring color.
/// @param $outer-ring-width [$outer-ring-width-default] - Outer focus ring width.
/// @param $outer-ring-color [$outer-ring-color-default] - Outer focus ring color.
/// @param $container-outer-padding-vertical [$container-outer-padding-default]
////    - The vertical distance between the focus ring and the container.
/// @param $container-outer-padding-horizontal [$container-outer-padding-default]
////    - The horizontal distance between the focus ring and the container.
/// @param $rtl-reflexive [false] - Whether to flip ring radius corners in RTL.
@mixin focus-ring(
  $query: feature-targeting.all(),
  $ring-radius: $ring-radius-default,
  $inner-ring-width: $inner-ring-width-default,
  $inner-ring-color: $inner-ring-color-default,
  $outer-ring-width: $outer-ring-width-default,
  $outer-ring-color: $outer-ring-color-default,
  $container-outer-padding-vertical: $container-outer-padding-default,
  $container-outer-padding-horizontal: $container-outer-padding-default,
  $rtl-reflexive: false
) {
  $feat-structure: feature-targeting.create-target($query, structure);

  $outer-ring-size: 100%;
  @if $outer-ring-width > 0 {
    $outer-ring-size: calc(100% + #{$outer-ring-width * 2});
  }

  @include feature-targeting.targets($feat-structure) {
    pointer-events: none;
    border: $inner-ring-width solid $inner-ring-color;
    @include _border-radius(
      _inner-ring-radius($ring-radius, $outer-ring-width),
      $rtl-reflexive: $rtl-reflexive
    );
    box-sizing: content-box;
    position: absolute;
    top: 50%;
    @include rtl.ignore-next-line();
    left: 50%;
    @include rtl.ignore-next-line();
    transform: translate(-50%, -50%);

    @include dom.forced-colors-mode($exclude-ie11: true) {
      border-color: CanvasText;
    }

    &::after {
      content: '';
      border: $outer-ring-width solid $outer-ring-color;
      @include _border-radius($ring-radius, $rtl-reflexive: $rtl-reflexive);
      display: block;
      position: absolute;
      top: 50%;
      @include rtl.ignore-next-line();
      left: 50%;
      @include rtl.ignore-next-line();
      transform: translate(-50%, -50%);
      height: $outer-ring-size;
      width: $outer-ring-size;

      @include dom.forced-colors-mode($exclude-ie11: true) {
        border-color: CanvasText;
      }
    }
  }

  @include focus-ring-offset(
    $container-outer-padding-vertical,
    $container-outer-padding-horizontal,
    $query: $query
  );
}

/// Customizes the color of the focus ring.
///
/// @param $inner-ring-color [$inner-ring-color-default] - Inner focus ring color.
/// @param $outer-ring-width [$outer-ring-width-default] - Outer focus ring width.
@mixin focus-ring-color(
  $inner-ring-color: $inner-ring-color-default,
  $outer-ring-color: $outer-ring-color-default,
  $query: feature-targeting.all()
) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    border-color: $inner-ring-color;

    &::after {
      border-color: $outer-ring-color;
    }
  }
}

/// Customizes the offset of the focus ring.
///
/// @param $container-outer-padding-vertical [$container-outer-padding-default]
////    - The vertical distance between the focus ring and the container.
/// @param $container-outer-padding-horizontal [$container-outer-padding-default]
////    - The horizontal distance between the focus ring and the container.
@mixin focus-ring-offset(
  $container-outer-padding-vertical: $container-outer-padding-default,
  $container-outer-padding-horizontal: $container-outer-padding-default,
  $offset: 0,
  $query: feature-targeting.all()
) {
  $container-size-vertical: 100%;
  $container-size-vertical-offset: $container-outer-padding-vertical + $offset *
    2;
  @if $container-size-vertical-offset != 0 {
    $container-size-vertical: calc(100% + $container-size-vertical-offset * 2);
  }
  $container-size-horizontal: 100%;
  $container-size-horizontal-offset: $container-outer-padding-horizontal +
    $offset * 2;
  @if $container-size-horizontal-offset != 0 {
    $container-size-horizontal: calc(
      100% + $container-size-horizontal-offset * 2
    );
  }

  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    height: $container-size-vertical;
    width: $container-size-horizontal;
  }
}

/// Customizes the border radius of the button focus ring.
///
/// @param {Number|List} $ring-radius - The border radius of the focus ring,
///     either a single value or a list of radius values to apply.
/// @param {Number} $outer-ring-width [$outer-ring-width] - Width of the outer
///     ring, required to compute the radius for the inner ring.
/// @param $rtl-reflexive [false] - Whether to flip ring radius corners in RTL.
@mixin focus-ring-radius(
  $ring-radius,
  $outer-ring-width: $outer-ring-width-default,
  $query: feature-targeting.all(),
  $rtl-reflexive: false
) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    @include _border-radius(
      _inner-ring-radius($ring-radius, $outer-ring-width),
      $rtl-reflexive: $rtl-reflexive
    );
    &::after {
      @include _border-radius($ring-radius, $rtl-reflexive: $rtl-reflexive);
    }
  }
}

/// Generates a border radius property.
/// @param $radius - The border radius
/// @param $rtl-reflexive - Whether to generate an RTL border radius if needed.
@mixin _border-radius($radius, $rtl-reflexive) {
  $has-multiple-corners: meta.type-of($radius) == 'list' and
    list.length($radius) > 1;
  // Even if $rtl-reflexive is true, only emit RTL styles if we can't easily
  // tell that the given radius is symmetrical
  $needs-flip: $rtl-reflexive and $has-multiple-corners;
  @include gss.annotate(
    (
      noflip: $needs-flip,
    )
  );
  border-radius: $radius;
  @if $needs-flip {
    @include rtl.rtl {
      @include gss.annotate(
        (
          noflip: true,
        )
      );
      border-radius: shape.flip-radius($radius);
    }
  }
}

/// Returns the inner focus ring's border radius given the outer ring's radius.
///
/// @param {Number|List} $ring-radius - The border radius of the focus ring,
///.    either a single value or a list of radius values to apply.
/// @param {Number} $outer-ring-width [$outer-ring-width] - Width of the outer
///     ring, required to compute the radius for the inner ring.
/// @return {Number|List} the inner focus ring's border radius.
@function _inner-ring-radius($ring-radius, $outer-ring-width) {
  $inner-ring-radius: null;
  @if meta.type-of($ring-radius) == 'list' {
    $inner-ring-radius: ();
    @each $radius in $ring-radius {
      $inner-radius: math.max($radius - $outer-ring-width, 0);
      $inner-ring-radius: list.append(
        $inner-ring-radius,
        $inner-radius,
        $separator: space
      );
    }
  } @else {
    $inner-ring-radius: calc($ring-radius - $outer-ring-width);
  }
  @return $inner-ring-radius;
}
